package action;

import Impl.FriendDaoImpl;
import Impl.UserDaoImpl;
import entity.Friend;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "AddFriend")
public class AddFriend extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("-----正在执行AddFriend---------");
        request.setCharacterEncoding("utf-8");
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        String school = request.getParameter("school");
        String email = request.getParameter("email");
        String phone =request.getParameter("phone");
        String method = request.getParameter("method");
        String uname = (String) request.getSession().getAttribute("uname");
        Friend friend = new Friend(name, sex,email,phone,school);
        FriendDaoImpl friendDao = new FriendDaoImpl();
        if (method!=null && method.equals("edit")){
            System.out.println("-----正在执行编辑操作--------");
            int fid = Integer.parseInt(request.getParameter("fid"));
            System.out.println(fid);
            friend.setFid(fid);
            friendDao.update(friend,uname);
        }else{
            System.out.println("--------正在执行新增操作----------");
            friendDao.save(friend,uname);
        }
        request.getRequestDispatcher("/friend.jhtml").forward(request,response);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
